#import "sprite_game_heart.h"

/*sprite_game_heart
*/
@implementation SpriteGameHeart

@synthesize heartPoint = heartPoint_;
@synthesize box2dBody = box2dBody_;
@synthesize box2dFixture = box2dFixture_;
@synthesize flagDark = flagDark_;
@synthesize flagDie = flagDie_;
@synthesize flagMouth = flagMouth_;
@synthesize flagMosquito = flagMosquito_;
@synthesize flagSwirl = flagSwirl_;
@synthesize enemyMosquitoHandle = enemyMosquitoHandle_;
@synthesize enemyMouthHandle = enemyMouthHandle_;
@synthesize swrilHandle = swirlHandle_;

- (id)initSpriteGameHeart:(int)HeartPoint
{
	NSString *_Path;

	if ((self = [super init]))
	{
		[self schedule:@selector(tick:)];
		[self schedule:@selector(tickDisapper:) interval:g_CfgGameHeartData.life_cycle];

		heartPoint_ = HeartPoint;
		self.scale = (float)HeartPoint / 10.0f;

		[self CreateBody];

		flagDark_ = false;
		flagDie_ = false;
		flagMouth_ = false;
		flagMosquito_ = false;
		flagSwirl_ = false;
		enemyMouthHandle_ = nil;
		enemyMosquitoHandle_ = nil;
		swirlHandle_ = nil;

		_Path = [NSString stringWithUTF8String:g_CfgGameHeartData.default_image_path];
		image_ = [[CCSprite alloc] initWithSpriteFrameName:_Path];
		[self addChild:image_];
		[self PlayAni:HEART_ANI_FLY];
	}
	return self;
}

- (void)dealloc
{
	if (NULL != box2dBody_)
	{
		[g_LayerGameObject PutBox2dBodyToDestroyPool:box2dBody_];
		box2dBody_ = NULL;
	}

	[super dealloc];
}

- (void)tick:(ccTime)Time
{
	if (NULL != box2dBody_)
	{
		box2dBody_->SetTransform([self ComputeBodyPosition], 0.0f);
	}
}

- (void)tickDisapper:(ccTime)Time
{
	if (flagMouth_)
	{
		enemyMouthHandle_.mouthHeartHandle = nil;
	}
	if (flagMosquito_)
	{
		enemyMosquitoHandle_.mosquitoHeartHandle = nil;
	}
	[g_LayerGameObject RemoveHeart:self];
}

- (void)CreateBody
{
	int i;
	int _Count;
	b2BodyDef _BodyDef;
	b2PolygonShape _PolygonShape;
	b2FixtureDef _FixtureDef;
	b2Vec2 *_Vertex;

	_BodyDef.type = b2_dynamicBody;
	box2dBody_ = CMgrGamePhysics::World()->CreateBody(&_BodyDef);

	_Count = g_CfgGameHeartData.box2d_collision_point_table.size();
	_Vertex = ptCreateCollisionVertex(&g_CfgGameHeartData.box2d_collision_point_table);
	for (i = 0; i < _Count; ++i)
	{
		_Vertex[i] = ptScaleVector_same(_Vertex[i], 1 / CMgrGamePhysics::Ratio() * self.scale);
	}
	_PolygonShape.Set(_Vertex, _Count);
	free(_Vertex);

	_FixtureDef.shape = &_PolygonShape;
	_FixtureDef.density = 1.0f;
	_FixtureDef.isSensor = true;

	box2dFixture_ = box2dBody_->CreateFixture(&_FixtureDef);
}

- (void)DestroyBody
{
	if (NULL != box2dBody_)
	{
		[g_LayerGameObject PutBox2dBodyToDestroyPool:box2dBody_];
		box2dBody_ = NULL;
		box2dFixture_ = NULL;
	}
}

- (b2Vec2)ComputeBodyPosition
{
	return ptConvertCGPoint(ptScalePoint_same(self.positionInPixels, 1 / CMgrGamePhysics::Ratio()));
}

- (void)Pause
{
	[self pauseSchedulerAndActions];
	[image_ pauseSchedulerAndActions];
}

- (void)Resume
{
	[self resumeSchedulerAndActions];
	[image_ resumeSchedulerAndActions];
}

- (bool)IsMyFixture:(b2Fixture *)Fixture
{
	if (NULL == box2dBody_)
	{
		return false;
	}
	else
	{
		return Fixture == box2dFixture_;
	}
}

- (void)PlayAni:(int)Type
{
	id _Animation;
	id _ActionFly;
	CCSequence *_Seq;
	id _ActionDie;
	CCCallFuncN *_CallBack;

	[self stopAllActions];
	switch (Type)
	{
	case HEART_ANI_FLY:
		_Animation = uiCreateAnimationObject(flagDark_ ? &g_CfgGameHeartData.ani_dark_fly : &g_CfgGameHeartData.ani_fly);
		_ActionFly = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:_Animation]];
		[image_ runAction:_ActionFly];
		break;
	case HEART_ANI_DIE:
		_Animation = uiCreateAnimationObject(flagDark_ ? &g_CfgGameHeartData.ani_dark_die : &g_CfgGameHeartData.ani_die);
		_ActionDie = [CCAnimate actionWithAnimation:_Animation];
		_CallBack = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackAniDie:)];
		_Seq = [CCSequence actions:_ActionDie, _CallBack, nil];
		[image_ runAction:_Seq];
		break;
	}
}

- (void)ActionToturial
{
	[self unschedule:@selector(tickDisapper:)];
}

- (void)ActionDie
{
	id _ActionMove;

	flagDie_ = true;
	if (flagMouth_)
	{
		enemyMouthHandle_.mouthHeartHandle = nil;
	}
	if (flagMosquito_)
	{
		enemyMosquitoHandle_.mosquitoHeartHandle = nil;
	}
	if (flagSwirl_)
	{
		[swirlHandle_ DetachmentHeart:self];
	}
	[self unschedule:@selector(tickDisapper:)];
	[self PlayAni:HEART_ANI_DIE];
	_ActionMove = [action_move_in_pixel actionWithDuration:g_CfgGameHeartData.ani_die.total_time position:g_LayerGameObject.startPos];
	[self runAction:_ActionMove];
}

- (void)ActionEat
{
	CCSequence *_Seq;
	id _Action;
	CCCallFuncN *_CallBack;

	flagDie_ = true;
	if (flagMouth_)
	{
		enemyMouthHandle_.mouthHeartHandle = nil;
	}
	if (flagMosquito_)
	{
		enemyMosquitoHandle_.mosquitoHeartHandle = nil;
	}
	if (flagSwirl_)
	{
		[swirlHandle_ DetachmentHeart:self];
	}
	_Action = [CCScaleTo actionWithDuration:1.0 scale:0.0f];
	_CallBack = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackActionEatOver:)];
	_Seq = [CCSequence actions:_Action, _CallBack, nil];
	[self runAction:_Seq];
}

- (void)ActionDark
{
	NSString *_Path;
	CCSpriteFrame *_Frame;

	flagDark_ = true;
	_Path = [NSString stringWithUTF8String:g_CfgGameHeartData.dark_image_path];
	_Frame = [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:_Path];
	[image_ setDisplayFrame:_Frame];
	[self PlayAni:HEART_ANI_FLY];
	[self unschedule:@selector(tickDisapper:)];
	[self schedule:@selector(tickDisapper:) interval:g_CfgGameHeartData.life_cycle];
}

- (void)ActionMoveToSwirl:(sprite_game_swirl *)Swirl And:(CGPoint)StartPosition And:(CGPoint)EndPosition
{
	float _Time;
	id _ActionMove;
	CCCallFuncN *_CallBackMoveOver;
	CCSequence *_Seq;

	flagSwirl_ = true;
	swirlHandle_ = Swirl;
	[self unschedule:@selector(tickDisapper:)];
	self.positionInPixels = StartPosition;
	_Time = ptPointDistance_CGPoint(StartPosition, EndPosition) / 80.0f;
	_ActionMove = [action_move_in_pixel actionWithDuration:_Time position:EndPosition];
	_CallBackMoveOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackMoveToSwirlOver:)];
	_Seq = [CCSequence actions:_ActionMove, _CallBackMoveOver, nil];
	[self PlayAni:HEART_ANI_FLY];
	[self runAction:_Seq];
}

- (void)CallBackAniDie:(id)Sender
{
	CMgrGameMission::Instance()->EventHeartGet(self);
	[g_LayerGameObject RemoveHeart:self];
}

- (void)CallBackActionEatOver:(id)Sender
{
	[g_LayerGameObject RemoveHeart:self];
}

- (void)CallBackMoveToSwirlOver:(id)Sender
{
	if (nil != swirlHandle_)
	{
		[swirlHandle_ DetachmentHeart:self];
	}
	[g_LayerGameObject RemoveHeart:self];
}

@end

/*sprite_game_star
*/
@implementation sprite_game_star

- (id)initSpriteGameStar
{
	if ((self = [super init]))
	{
		[self schedule:@selector(tick:)];

		[self CreateBody];

		image_ = [CCSprite spriteWithSpriteFrameName:[NSString stringWithUTF8String:g_CfgGameMenuStarOn]];
		[self addChild:image_];
		image_.opacity = 0;
	}
	return self;
}

- (void)tick:(ccTime)Time
{
	if (NULL != box2dBody_)
	{
		box2dBody_->SetTransform([self ComputeBodyPosition], 0.0f);
	}
}

- (void)CreateBody
{
	b2BodyDef _BodyDef;
	b2PolygonShape _PolygonShape;
	b2FixtureDef _FixtureDef;

	_BodyDef.type = b2_dynamicBody;
	box2dBody_ = CMgrGamePhysics::World()->CreateBody(&_BodyDef);

	_PolygonShape.SetAsBox((float)20 / CMgrGamePhysics::Ratio(), (float)20 / CMgrGamePhysics::Ratio());

	_FixtureDef.shape = &_PolygonShape;
	_FixtureDef.density = 1.0f;
	_FixtureDef.isSensor = true;

	box2dFixture_ = box2dBody_->CreateFixture(&_FixtureDef);
}

- (void)DestroyBody
{
	if (NULL != box2dBody_)
	{
		[g_LayerGameObject PutBox2dBodyToDestroyPool:box2dBody_];
		box2dBody_ = NULL;
		box2dFixture_ = NULL;
	}
}

- (b2Vec2)ComputeBodyPosition
{
	return ptConvertCGPoint(ptScalePoint_same(self.positionInPixels, 1 / CMgrGamePhysics::Ratio()));
}

- (bool)IsMyFixture:(b2Fixture *)Fixture
{
	if (NULL == box2dBody_)
	{
		return false;
	}
	else
	{
		return Fixture == box2dFixture_;
	}
}

- (void)HitStar
{
	switch (image_.opacity)
	{
	case 0: image_.opacity = 100; break;
	case 100: image_.opacity = 200; break;
	case 200: image_.opacity = 255; break;
	case 255:
		/*reocrd
		*/
		CMgrGameMission::Instance()->mission_recrod.flag_star_special = true;

		[self ActionGet];
		break;
	default:;
	}
}

- (void)ActionGet
{
	id _Action;
	CCCallFuncN *_CallBack;
	CCSequence *_Seq;

	_Action = [action_move_in_pixel actionWithDuration:1.0f position:g_SpriteGameBow.positionInPixels];
	_CallBack = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackActionGet:)];
	_Seq = [CCSequence actions:_Action, _CallBack, nil];
	[self runAction:_Seq];
}

- (void)CallBackActionGet:(id)Sender
{
	[g_LayerGameObject RemoveStar];
}

@end